#!/usr/bin/env perl

# This looks in /opt/gri/backend-privkeys and will use that
# private key for communication to the specified backend - 
# defaulting to local hostname if none is specified.

use strict;
use warnings;
use Sys::Hostname;
use Getopt::Long;

BEGIN {
	push @::INC,'../libs' if -d '../libs';
	push @::INC,'libs' if -d 'libs';
}

use gri_frontend;

$::f=undef;
$::f='../globals_config.xml' if -e '../globals_config.xml';
$::f='globals_config.xml' if -e 'globals_config.xml';

if(! $::f) {
	print STDERR "Error: Failed to find 'globals_config.xml' file.\n";
	exit 1;
}

# Load in the configuration file ...

my $ff=new gri_frontend($::f);
if(! $ff) {
	print STDERR "Error: Failed to load in frontend.\n";
	exit 1;
}

$::ARG_hostname=hostname();
if(!GetOptions(
	"hostname=s"	=> \$::ARG_hostname,
)) {
	print STDERR "Usage: backend_client.pl [--hostname] msg\n";
	exit 1;
}

$::msg="GET_CTIME";
if(@::ARGV) {
	$::msg=join(' ',@::ARGV);
}

{
	my ($a,$b)=$ff->send_backend_request($::ARG_hostname,$::msg);
	if(!$a) {
		print STDERR "Error: $_\n" foreach(@$b);
		exit 1;
	}
	print "Result: $b\n";
	exit 0;
}

